package com.wash.shoes.controller.app;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.wash.shoes.common.enums.FulFillEnum;
import com.wash.shoes.common.enums.OrderStatusEnum;
import com.wash.shoes.domain.dto.ShoeImgDto;
import com.wash.shoes.domain.dy.request.DyServiceUpdateRequest;
import com.wash.shoes.domain.mtbusy.MtUpdateFulFillInfo;
import com.wash.shoes.entity.OrderPo;
import com.wash.shoes.entity.ShoePo;
import com.wash.shoes.service.OrderService;
import com.wash.shoes.service.ShoeService;
import com.wash.shoes.util.AdminUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author ljg
 * @since 2024-08-28
 */
@Log4j2
@Api(tags = {"APP-鞋子"})
@RestController
@RequestMapping("/app/shoe")
public class ShoeAController {

    @Autowired
    ShoeService shoeService;

    @Autowired
    OrderService orderService;

    @Autowired
    MtUpdateFulFillInfo mtUpdateFulFillInfo;

    @Autowired
    DyServiceUpdateRequest dyServiceUpdateRequest;

    @ApiOperation("鞋子入库--如果返回true订单鞋子上传完毕 false未上传完毕")
    @PostMapping("/shoeCheckIn")
    @Transactional(rollbackFor = Exception.class)
    public R<Boolean> shoeCheckIn(@RequestBody ShoeImgDto shoeImgDto) {
        ShoePo shoePo = shoeService.getById(shoeImgDto.getShoeId());
        // 设置鞋子的图片
        shoePo.setPicture(shoeImgDto.getShoeImg());
        shoePo.setWashType(shoeImgDto.getWashType());
        // 设置鞋子为清洗中
        shoePo.setStatus(1);
        // 查询是否有为清洗的鞋子
        shoeService.updateById(shoePo);
        // 订单的ID
        List<ShoePo> allShoeList = shoeService.getByOrderId(shoePo.getOrderId());
        boolean finishUploadImg = false;
        for (ShoePo shoePoDb : allShoeList) {
            if (shoePoDb.getStatus() == 0) {
                finishUploadImg = false;
                break;
            } else {
                finishUploadImg = true;
            }
        }
        // 如果已经完成所有的鞋子上传,修改订单的状态
        if (finishUploadImg) {
            OrderPo orderPo = orderService.getById(shoePo.getOrderId());
            orderPo.setStatus(OrderStatusEnum.CLEANING_IN_PROCESS.getStatus());
            // 清洗中的订单 抖音核销
            dyServiceUpdateRequest.updateFullFill(orderPo, 4);
            //PAD入库没有设置os_order中的入库人与入库时间，这里更新设置进去
            log.info("PAD入库测试获取登录的用户为:" + AdminUserUtil.getLoginUserName());
            if(StringUtils.isBlank(orderPo.getCheckInPerson()) && Objects.isNull(orderPo.getCheckInTime())) {
                orderPo.setCheckInPerson(AdminUserUtil.getLoginUserName());
                orderPo.setCheckInTime(new Date());
            }
            orderService.updateById(orderPo);
            // 如果是线上的订单，mt的订单,就需要更新美团的状态为衣服清洗中
            if (orderPo.getComeFrom().equals("2")) {
                log.info("手机入库 更新美团的物流信息为[清洗中(洗衣)]:" + orderPo.getCustomPhone());
                mtUpdateFulFillInfo.fulfillInfo(orderPo.getId(), FulFillEnum.CLEANING, "刷鞋匠", "13892920924");
            }
            return R.ok(true);
        }
        return R.ok(false);
    }

}

